package com.extra_archive.project.iisu.view.list 
{
	import iisu.iisuEvent.ZoomEvent;

	import com.extra_archive.framework.core.mvc.controller.IController;
	import com.extra_archive.framework.core.mvc.model.IModel;
	import com.extra_archive.framework.core.mvc.view.CompositeView;
	import com.extra_archive.project.iisu.controller.ARController;
	import com.extra_archive.project.iisu.model.ARModel;
	import com.extra_archive.project.iisu.model.ARModelEvent;
	import com.extra_archive.project.iisu.view.IARState;
	import com.greensock.TweenMax;
	import com.greensock.easing.Back;
	import com.greensock.easing.Cubic;
	import com.greensock.easing.Elastic;

	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	/**
	 * @class name : ARListView
	 * @author : implict (http://extra-archive.com)
	 * @date : 2010. 4. 29.
	 * @description :
	 */
	public class ARListVerticalView extends CompositeView implements IARState
	{
		private var _isActive : Boolean;
		private var _planes : Vector.<Sprite> = new Vector.<Sprite>();
		private var _overredIndex : int = -1; // -1 : none overred
		
		public function ARListVerticalView(model : IModel = null, controller : IController = null)
		{
			super(model, controller);
			
			initialize();
		}
		
		//--------------------------------------------------------------------------
		//
		//    Public Methods
		//
		//--------------------------------------------------------------------------
		
		override public function initialize() : void
		{
			trace(this + " : initialize");
			
			createList();
			
			ARModel(_model).gesture.addEventListener(ZoomEvent.ZOOM_IN, onZoomIn);
		}

		override public function update(event : Event = null) : void
		{
			if (!_isActive) return;
			
		    moveList();
			hitTestArea();
			hitTestPlane();
		}

		override public function toString() : String
		{
			return "com.extra_archive.project.iisu.view.list.ARListView";
		}
		
		public function changeState() : void
		{
			dispatchEvent(new ARModelEvent(ARModelEvent.LIST_TO_DETAIL));
		}
		
		public function live() : void
		{
			
		}
		
		public function kill() : void
		{
		}
		
		//--------------------------------------------------------------------------
		//
		//    Event Listeners
		//
		//--------------------------------------------------------------------------
		
		/**
		 * 등장 모션 completed
		 */
		private function onIntroComplete(count : uint) : void
		{
			if (count == ARModel(_model).assetManager.listImgs.length - 1) 
			{
				_isActive = true;
				
				for each (var plane : Sprite in _planes)
				{
					TweenMax.to(plane, 0.3, {alpha : 0.5, ease: Cubic.easeOut});
				}
			}
		}
		
		/**
		 * effect of plane overred 
		 */
		private function onOverPlane(index : uint) : void 
		{
			/**
			 * 선택된 plane의 크기 및 투명도 변화
			 */
			var selectedPlane : Sprite = Sprite(getChildByName("plane" + index));
			setChildIndex(selectedPlane, _planes.length - 1);
			TweenMax.to(selectedPlane, 1, {alpha : 1, width : 170, height : 170, ease : Elastic.easeOut}); 
//			if (_selectedIndex != index && _selectedIndex != -1) TweenMax.to(getChildByName("plane" + _selectedIndex), 0.4, {alpha : 0.5, width : 100, height : 100, ease : Back.easeOut}); 
			_overredIndex = index;

			/**
			 * 손과의 거리차에 따른 plane들의 크기 및 투명도 변화
			 */
			var distance : uint;
			var ratio : Number;
			var minSize : uint = 20;
			var maxSize : uint = 100;
			var size : uint;
			
			for each (var plane: Sprite in _planes)
			{
				if (plane.name != "plane" + index)
				{
					distance = Math.abs(_model.applicationContainer.stage.mouseY - localToGlobal(new Point(plane.x, plane.y)).y);
					distance = Math.abs(distance - 768);
					ratio = distance / 768;
					size = uint((maxSize - minSize) * ratio + minSize);
					TweenMax.to (plane, 0.4, {alpha : ratio, width : size , height : size, ease: Cubic.easeOut});
				}
			}
		}
		
		/**
		 * select plane
		 */
		private function onSelectPlane(index : uint) : void
		{
			_isActive = false;
			ARController(_controller).changeSection(index);
			TweenMax.to (this, 0.5, {y : this.y - 840, ease : Back.easeIn, onComplete: changeState});
		}
		
		private function onZoomIn(event : ZoomEvent) : void 
		{
			onSelectPlane(_overredIndex);
		}

		//--------------------------------------------------------------------------
		//
		//    Private Methods
		//
		//--------------------------------------------------------------------------
		
		private function createList() : void
		{
			var plane : Sprite;
			var bitmap : Bitmap;
			var i : uint, len : uint = ARModel(_model).assetManager.listImgs.length;
			
			for (i; i < len; ++i)
			{
				plane = new Sprite();
				bitmap = ARModel(_model).assetManager.listImgs[i];
				bitmap.smoothing = true;
				bitmap.x = -bitmap.width/2;
				bitmap.y = -bitmap.height/2;
				plane.addChild(bitmap);
				plane.name = "plane" + i;
				plane.width = 100;
				plane.height = 100;
				plane.x = (_model.applicationContainer.stage.stageWidth - plane.width) / 2 + plane.width / 2;
				plane.alpha = 0;
				addChild(plane);
				_planes.push(plane);
				
				TweenMax.to(plane, 0.4, {delay : i * 0.1 + 0.2, alpha : 1, y : 768 - 110 * i, ease: Cubic.easeOut, onComplete: onIntroComplete, onCompleteParams : [i]});
			}
		}
		
		/**
		 * 손 위치에 따른 리스트 이동
		 */
		private function moveList() : void 
		{
			this.y += int(-(_model.applicationContainer.mouseY - _model.applicationContainer.stage.stageHeight / 2) / (_model.applicationContainer.stage.stageHeight / 2) * 20);
		}
		
	   /**
		* 경계면에 위치한 plane 이동
		*/
		private function hitTestArea() : void 
		{
			if (localToGlobal(new Point(_planes[0].x, _planes[0].y)).y > 818)
			{
				_planes[0].y = _planes[_planes.length - 1].y - 110;
				_planes.push(_planes.shift());
				_planes.length = ARModel(_model).assetManager.listImgs.length;
			}
			
			if (localToGlobal(new Point(_planes[_planes.length - 1].x, _planes[_planes.length - 1].y)).y < -50)
			{
				_planes[_planes.length - 1].y = _planes[0].y + 110;
				_planes.unshift(_planes.pop());
				_planes.length = ARModel(_model).assetManager.listImgs.length;
			}
		}

		/**
		 * 손 Y위치에 있는 plane 파악
		 */
		private function hitTestPlane() : void 
		{
			var rect : Rectangle;
			var planeGlobalPoint : Point;
			var i : uint, len : uint = ARModel(_model).assetManager.listImgs.length;
			
			for (i; i < len; ++i)
			{
				planeGlobalPoint = localToGlobal(new Point(getChildByName("plane" + i).x, getChildByName("plane" + i).y));
				rect = new Rectangle(planeGlobalPoint.x - 512, planeGlobalPoint.y - 50, 1024, 100);
				if (rect.contains(_model.applicationContainer.stage.mouseX, _model.applicationContainer.stage.mouseY)) onOverPlane(i);
			}
		}
	}
}
